package work.MrSun.xyq.util.isComment;

import work.MrSun.xyq.model.XyqAttend;
import work.MrSun.xyq.model.XyqComment;
import work.MrSun.xyq.model.XyqNewMessage;
import work.MrSun.xyq.model.XyqUser;
import work.MrSun.xyq.service.XyqAttendService;
import work.MrSun.xyq.service.XyqCommentService;
import work.MrSun.xyq.service.XyqNewMessageService;
import work.MrSun.xyq.service.XyqUserService;

public class IsComment {

    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public IsComment isTrue(Integer userId, Integer messageId, String userComment, Integer messageUserId, XyqUserService mangoUserService, XyqCommentService mangoCommentService, XyqNewMessageService mangoNewMessageService, XyqAttendService mangoAttendService) {
        IsComment isComment = new IsComment();
        isComment.setCode(500);
        XyqUser user = mangoUserService.getById(userId);
        if (user == null) {
            isComment.setCode(400);
            return isComment;
        }

        if(user.getUserAllow()!=1){
            isComment.setCode(301);
            return isComment;
        }

        XyqComment mangoComment = new XyqComment();
        mangoComment.setMessageId(messageId);
        mangoComment.setUserId(userId);
        mangoComment.setCommentDetail(userComment);
        mangoCommentService.add(mangoComment);

      if(userId!=messageUserId) {
          XyqNewMessage mangoNewMessage = new XyqNewMessage();
          mangoNewMessage.setUserId(messageUserId);
          mangoNewMessage.setNewMessageType(1);
          mangoNewMessage.setNewMessageDetail(userComment);
          mangoNewMessage.setMessageId(messageId);
          mangoNewMessageService.add(mangoNewMessage);
      }
        XyqAttend mangoAttend = new XyqAttend();

        mangoAttend.setMessageId(messageId);
        mangoAttend.setUserId(userId);

        if (mangoAttendService.findCount(mangoAttend) != 0) {
            isComment.setCode(200);
            return isComment;
        }


        mangoAttendService.add(mangoAttend);

        isComment.setCode(200);
        return isComment;
    }
}
